import ACTIONS from '../actions/actionTypes';

import type { ICustomer } from '@/services/customer.d';

interface IinitialState {
  isFetching: boolean;
  requestRes: null | string;
  customerList: ICustomer[];
  customerDetail: null | ICustomer;
  pageInfo: {
    page: number;
    pageSize: number;
    totalPages: number;
    total: number;
  };
}

const initialState: IinitialState = {
  isFetching: false,
  requestRes: null,
  customerList: [],
  customerDetail: null,
  pageInfo: {
    page: 1,
    pageSize: 10,
    totalPages: 0,
    total: 0,
  },
};

export default function customerReducer(state = initialState, action) {
  switch (action.type) {
    case ACTIONS.FETCHING:
      return Object.assign({}, state, {
        isFetching: action.isFetching,
      });
    case ACTIONS.INIT_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerList: action.payload.list,
        pageInfo: action.payload.pageInfo,
      });
    case ACTIONS.GET_DETAIL_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerDetail: action.payload,
      });
    case ACTIONS.UPDATE_OP_MESSAGE:
      return Object.assign({}, state, {
        requestRes: action.payload,
      });
    case ACTIONS.ADD_CUSTOMER:
    case ACTIONS.UPDATE_CUSTOMER:
    case ACTIONS.REMOVE_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        requestRes: action.payload,
      });
    default:
      return state;
  }
}
